package pnu.ssm.genetic.algorithm;
// 세대 (Population) : 염색체 (Individual) 을 집합체
public class Population {
	
	Individual individuals[];
	
	// Create a population
	public Population(int populationSize, boolean initialise) {
		individuals = new Individual[populationSize];
		if(initialise){
			// Loop and create individuals
			for(int i=0; i<size(); i++){
				Individual newIndividual = new Individual();
				newIndividual.generateIndividual();
				saveIndividual(i, newIndividual);
			}
		}
		// initialise 가 false 일 경우는 염색체들 (individuls)이 NULL 상태이다.
	}
	
	// Getters
	public Individual getIndividual(int index){
		return individuals[index];
	}
	
	public Individual getFittest(){
		Individual fittest = individuals[0];
		for(int i=0; i<size(); i++){
			if(fittest.getFitness() <= getIndividual(i).getFitness()){
				fittest = getIndividual(i);
			}
		}
		return fittest;
	}	
	
	// Public methods
	// Get population size
	public int size(){
		return individuals.length;
	}
	
	// Save individual
	public void saveIndividual(int index, Individual indiv){
		individuals[index] = indiv;
	}
}







